package com.youth.koodoReaderManager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youth.koodoReaderManager.entity.Bookmark;
import com.youth.koodoReaderManager.service.BookmarkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/bookmarks")
@Api(tags = "书签管理", description = "提供书签的增删改查及分页查询接口")
public class BookmarkController {

    private final BookmarkService bookmarkService;

    @Autowired
    public BookmarkController(BookmarkService bookmarkService) {
        this.bookmarkService = bookmarkService;
    }

    @PostMapping
    @ApiOperation(value = "创建新书签", notes = "添加新的书签记录")
    public boolean createBookmark(@RequestBody Bookmark bookmark) {
        return bookmarkService.save(bookmark);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新书签信息", notes = "根据书签ID更新书签信息")
    public boolean updateBookmark(@PathVariable String id, @RequestBody Bookmark bookmark) {
        return bookmarkService.updateById(bookmark);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除书签", notes = "根据书签ID删除书签记录")
    public boolean deleteBookmark(@PathVariable String id) {
        return bookmarkService.removeById(id);
    }

    @GetMapping
    @ApiOperation(value = "分页查询书签", notes = "根据查询条件分页获取书签列表")
    public IPage<Bookmark> pageQueryBooks(@RequestParam(defaultValue = "1") int current,
                                          @RequestParam(defaultValue = "10") int size,
                                          @RequestBody(required = false) Bookmark bookmark) {
        // 创建分页对象
        IPage<Bookmark> page = new Page<>(current, size);
        // 调用服务层执行分页查询
        return bookmarkService.pageQuery(page, bookmark);
    }
}